package com.kdtech.analyse.NewsPaper;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.RegexUtils;
import com.kdtech.utils.StringUtils;

/**
 * http://epaper.lnd.com.cn/html/tlrb/20121204/index.html 铁岭日报
 * http://epaper.lnd.com.cn/html/sdsb/20121204/index.html 时代商报
 * http://epaper.lnd.com.cn/html/lnrb/20121204/index.html 辽宁日报
 * http://epaper.lnd.com.cn/html/lswb/20121204/index.html 辽沈晚报
 * http://epaper.lnd.com.cn/html/bdcb/20121204/index.html 辽宁半岛晨报
 * http://epaper.lnd.com.cn/html/lswbasb/20121204/index.html 辽沈晚报鞍山
 * http://epaper.lnd.com.cn/html/lswbtlb/20121204/index.html 辽沈晚报铁岭
 *
 * @author KK
 *
 */
public class LndNewsPaperAnalyse implements AnalyseNews {

	
	public boolean isDetailPage(String url) {
		String[] regex = {
				"http://epaper.lnd.com.cn/html/(tlrb|sdsb|lnrb|lswb|bdcb|lswbasb|lswbtlb)/[0-9]{8}/(tlrb|sdsb|lnrb|lswb|bdcb|lswbasb|lswbtlb)[0-9]*.html",
				};
		return RegexUtils.matchAny(url, regex);
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {

		String html = urlMeta.getHtml();
		String url = urlMeta.getUrl();


		String title = null;
		String content = null;
		Long date = null;
		String dateStr = null;
		Document doc = Jsoup.parse(html);

		title =doc.select("table#Table17 tbody tr td").text();
		dateStr = url;
		date=DateUtils.matchDate(dateStr);

		content =HtmlCleaner.getContentHtml(url,doc.select("div#copytext"));

			NewsMeta newspaper = new NewsMeta();
			newspaper.setUrl(url);
			newspaper.setTitle(StringUtils.trimSpace(title));
			newspaper.setContent(StringUtils.trimSpace(content));
			newspaper.setDate(date);
			return newspaper;


	}

	
	public boolean isTaskPage(String url) {
		String[] regex = {
				"http://epaper.lnd.com.cn/html/(tlrb|sdsb|lnrb|lswb|bdcb|lswbasb|lswbtlb)/[0-9]{8}/(index.html|menu.html)",
				"http://epaper.lnd.com.cn/html/(tlrb|sdsb|lnrb|lswb|bdcb|lswbasb|lswbtlb)/[0-9]{8}/(tlrb|sdsb|lnrb|lswb|bdcb|lswbasb|lswbtlb)[AB][0-9]*.html",
				};
		return RegexUtils.matchAny(url, regex);
	}

	
}
